import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:tetris/panel/PagePortrait.dart';
import 'package:tetris/tool/ToastUtils.dart';

import 'gamer/gamer.dart';
import 'material/audios.dart';

final RouteObserver<ModalRoute> routeObserver = RouteObserver<ModalRoute>();

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {

    var materialView = MaterialApp(
      title: '经典回忆',
      debugShowCheckedModeBanner: false, // 不显示右上角的 debug 标记
      navigatorObservers: [routeObserver], // 导航监听
      theme: ThemeData(
        primarySwatch: Colors.red,
      ),
      home: HomePage(),
    );
    var safe = SafeArea(
      child:materialView,
    );
    return materialView ;
  }
}

class HomePage extends StatefulWidget {
  HomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  State<StatefulWidget> createState() {
    return _HomePageState();
  }
}

class _HomePageState extends State<HomePage>  {

  @override
  Widget build(BuildContext context) {
    // todo 双击退出
    DateTime _lastPressedAt= DateTime(2000, 1, 2);
    var exitView =WillPopScope(
      onWillPop: () async {
        if ( DateTime.now().difference(_lastPressedAt) > Duration(seconds: 2)) {
          _lastPressedAt = DateTime.now();
          ToastUtils.toastMsg("双击退出", context ,duration: 2 );
        } else {
          SystemNavigator.pop(animated: true); // 直接退出APP
        }
        return false ;
      },
      child: Sound(child: Game(child: PagePortrait( )))  ,
    );
    return exitView;
  }


}
